package com.yumeng.common.utils;

import com.yumeng.common.api.ApiCode;
import com.yumeng.common.api.ApiResult;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import java.io.IOException;

/**
 * @author wxd
 * @date 2025/2/18 14:31
 */
public class ResponseUtils {

    /**
     * 输出json
     *
     * @param response
     * @param status
     * @param apiCode
     * @param data
     * @param message
     * @throws IOException
     */
    public static void writeJson(HttpServletResponse response, HttpStatus status, ApiCode apiCode, Object data, String message) throws IOException {
        status = status != null ? status : HttpStatus.OK;
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setStatus(status.value());
        ApiResult result = ApiResult.resultDataMsg(apiCode, data, message);
        response.getWriter().write(JacksonUtils.toJsonString(result));
    }

    public static void writeJson(HttpServletResponse response, ApiCode apiCode, Object data, String message) throws IOException {
        writeJson(response, HttpStatus.OK, apiCode, data, message);
    }

    public static void writeJson(HttpServletResponse response, ApiCode apiCode, String message) throws IOException {
        writeJson(response, HttpStatus.OK, apiCode, null, message);
    }

    public static void writeJson(HttpServletResponse response, HttpStatus status, ApiCode apiCode, String message) throws IOException {
        writeJson(response, status, apiCode, null, message);
    }

}
